<template>
	<view>
		<view class="selfuser">
			<view class="text">用户编号:</view>
			<input class="input" type="text" v-model="selfuser" placeholder-class="placehoder" placeholder="自己的用户编号" />
		</view>
		<view class="selfuser">
			<view class="text">对方编号:</view>
			<input class="input" type="text" v-model="touser" placeholder-class="placehoder" placeholder="对方的用户编号" />
		</view>
		<view class="btnview">
			<button class="btn white" @click="goRoom">开始聊天</button>
			<button class="btn white" @click="change">交换用户</button>
			<button class="btn white" @click="exit">退出</button>
		</view>
	</view>
</template>

<script>
	import store from '@/store/index.js'
	export default {
		data() {
			return {
				selfuser: 'user1', //自己的用户编号
				touser: 'user2', //对方的用户编号
				selfInfo: {}, //自己的对象
				otherInfo: {}, //对方的对象
			}
		},
		methods: {
			goRoom() {
				var self = this;
				if (!self.selfuser || self.selfuser.length == 0) {
					self.$util.showToast("请输入自己的用户编号");
					return;
				}
				if (!self.touser || self.touser.length == 0) {
					self.$util.showToast("请输入对方的用户编号");
					return;
				}
				self.$lobster.userdata.forEach(item => {
					if (item.userId == self.selfuser) {
						self.selfInfo = item;
					}
					if (item.userId == self.touser) {
						self.otherInfo = item;
					}
				});
				if (!self.selfInfo.userId || !self.otherInfo.userId) {
					self.$util.showToast('用户不存在');
					return;
				}
				if (!store.state.im.isLogin) {
					if (!store.state.im.isSDKReady) {
						self.$lobster.im.initSDK(() => {
							self.$lobster.im.login(self.selfuser, (result) => {
								console.log(JSON.stringify(result));
							     self.$util.navigateTo('/pages/im/imRoom?userId=' + self.selfuser +
									'&touser=' + self.touser);
							});
						});
					}else{
						self.$lobster.im.login(self.selfuser, (result) => {
							console.log(JSON.stringify(result));
							self.$util.navigateTo('/pages/im/imRoom?userId=' + self.selfuser +
								'&touser=' + self.touser);
						});
					}
				} else {
					uni.navigateTo('/pages/im/imRoom?userId=' + self.selfuser + '&touser=' + self.touser);
				}
			},
			change(){
				var one = this.selfuser;
				var two = this.touser;
				
				this.selfuser = two;
				this.touser = one;
			},
			exit() {
				console.log("11111");
				// #ifdef APP-PLUS
				console.log("2222");
				this.$lobster.im.loginout();
				// #endif
			},

		}
	}
</script>

<style lang="scss">
	.selfuser {
		display: flex;
		align-items: center;
		padding: 10rpx;
		height: 80rpx;
	}

	.text {
		font-size: 32rpx;
		line-height: 60rpx;
		height: 80rpx;
		align-items: center;
		display: flex;
		padding-right: 20rpx;
	}

	.input {
		font-size: 32rpx;
		line-height: 60rpx;
		flex: 1;
		border: 2rpx #eee solid;
		height: 80rpx;
	}

	.btnview {
		margin: 20rpx;
	}

	.btn {
		box-sizing: border-box;
		//width: 100%;
		height: 80rpx;
		border-radius: 10rpx;
		font-size: 32rpx;
		line-height: 80rpx;
		margin: 20rpx;
	}

	.white {
		color: #ffffff;
		background-color: #4F91E2;
	}

	.placehoder {
		padding-left: 10rpx;
	}
</style>
